﻿using System;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.mediator;
using SharpSqlAnalyzer.pp.mediator.type;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.processor.type.select
{
	// Token: 0x0200022F RID: 559
	public class JoinOnProcessor : AbstractProcessor
	{
		// Token: 0x060012C7 RID: 4807 RVA: 0x0031A288 File Offset: 0x00318488
		public override void process(TParseTreeNode nodeArg)
		{
			TSelectSqlStatement tselectSqlStatement = nodeArg as TSelectSqlStatement;
			bool parameter = this.getParameter<bool>(typeof(bool));
			bool parameter2 = this.getParameter<bool>(typeof(bool), 1);
			TTableList tables = tselectSqlStatement.tables;
			TJoinList joins = tselectSqlStatement.joins;
			if (tables.size() == 0)
			{
				return;
			}
			int length = SourceTokenOperator.curColumnNumberVT(tables.getElement(0).startToken);
			for (int i = 0; i < joins.size(); i++)
			{
				TJoin join = joins.getJoin(i);
				for (int j = 0; j < join.JoinItems.size(); j++)
				{
					TJoinItem joinItem = join.JoinItems.getJoinItem(j);
					if (joinItem != null)
					{
						TSourceToken startToken = joinItem.startToken;
						SourceTokenOperator.removeAllBeforeTokenVT(this.Option, startToken);
						TSourceToken beginJoinToken = JoinOnProcessor.getBeginJoinToken(joinItem);
						if (beginJoinToken != null)
						{
							SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, beginJoinToken);
							SourceTokenOperator.addBefore(this.Option, beginJoinToken, SourceTokenOperator.createReturnSourceToken());
							if (parameter2)
							{
								KeywordAlignMediator mediator = MediatorFactory.getMediator<KeywordAlignMediator>(typeof(KeywordAlignMediator), this.Option.sessionId);
								SourceTokenOperator.addBefore(this.Option, beginJoinToken, SourceTokenOperator.createWhitespaceSourceToken(mediator.CurLevelIndentLen));
							}
							else
							{
								SourceTokenOperator.addBefore(this.Option, beginJoinToken, SourceTokenOperator.createWhitespaceSourceToken(length));
							}
						}
						if (joinItem.Table.Subquery != null)
						{
							ExpressionProcessor.processParenthesesNodeInSubQuery(this.Option, joinItem.Table.Subquery, joinItem.Table.Subquery.getStartToken(), joinItem.Table.Subquery.getEndToken());
						}
						if (joinItem.OnCondition != null)
						{
							TSourceToken tsourceToken = SourceTokenSearcher.lastSelectedNotWhitespaceAndReturnToken(joinItem.OnCondition.startToken, "on");
							if (tsourceToken != null && parameter)
							{
								SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken);
								SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
								SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(length));
							}
						}
					}
				}
				if (join.Table != null && join.Table.Subquery != null)
				{
					ExpressionProcessor.processParenthesesNodeInSubQuery(this.Option, join.Table.Subquery, join.Table.Subquery.startToken, join.Table.Subquery.endToken);
				}
			}
		}

		// Token: 0x060012C8 RID: 4808 RVA: 0x0031A4C8 File Offset: 0x003186C8
		public static TSourceToken getBeginJoinToken(TJoinItem lcitem)
		{
			TSourceToken startToken = lcitem.startToken;
			TSourceToken tsourceToken = null;
			if (lcitem.JoinType == EJoinType.cross || lcitem.JoinType == EJoinType.crossapply)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 5, "cross");
			}
			else if (lcitem.JoinType == EJoinType.full || lcitem.JoinType == EJoinType.fullouter)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 10, "full");
			}
			else if (lcitem.JoinType == EJoinType.inner)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 5, "inner");
				if (tsourceToken == null)
				{
					tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 5, "join");
				}
			}
			else if (lcitem.JoinType == EJoinType.left || lcitem.JoinType == EJoinType.leftouter)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 10, "left");
			}
			else if (lcitem.JoinType == EJoinType.natural)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 5, "natural");
			}
			else if (lcitem.JoinType == EJoinType.nested)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 5, "nested");
			}
			else if (lcitem.JoinType == EJoinType.right || lcitem.JoinType == EJoinType.rightouter)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 10, "right");
			}
			else if (lcitem.JoinType == EJoinType.outerapply)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 5, "outer");
			}
			else if (lcitem.JoinType == EJoinType.join)
			{
				tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 5, "join");
			}
			return tsourceToken;
		}
	}
}
